<?php
declare (strict_types = 1);

namespace app\command;

use app\model\Order;
use app\model\Promotion;
use app\util\CacheHelper;
use app\util\MengChen;
use app\util\Yizun;
use think\facade\Db;

class QueryOrderCommand extends BaseCommand
{
    /**
     * @var string 指令名称
     */
    protected $scriptName = "queryOrder";


    /**
     * 查询订单修改订单信息
     * @return bool
     */
    protected function _execute()
    {
        $version = '1.0';
        $locale = 'zh_CN';
        $messageFormat = 'json';
        $method = 'tradeService.queryOrderLogistics';
        $YizunM = new Yizun($method, $messageFormat, $locale, $version);
        $redis = new CacheHelper();

        Db::table('order')->where(['is_over'=>0,"supplier_id"=>1])->order('create_time','desc')->chunk(100, function($orders) use ($redis,$YizunM) {
            $reportKey = config('cachekeys.user_report');
            $reportJHKey = config('cachekeys.user_report_jh_num');
            $reportCZKey = config('cachekeys.user_report_cz_num');
            $jh_money = $cz_money = 0;
            foreach ($orders as $order) {
                $id = $order['id'];
                if (!$redis->lock("commit:$id",5)) {
                    return;
                }

                $params = json_encode([
                    'data' => [[
                        'merchantId' => $order['merchant_id'],
                        'tradeId' => $order['trade_id'],
                    ]]
                ]);
                $res = $YizunM->query($params);

                if (!$res['code'] && !empty($res['data'])) {

                    //客户换号 派送员现场换
                    if (isset($res['data'][0]['phoneNum']) && !empty($res['data'][0]['phoneNum'])) {
                        $order['buy_phone'] = $res['data'][0]['phoneNum'];
                    }
                    //$order['buy_phone'] = $res['data'][0]['phoneNum'] ?? '';
                    $order['logistics_company'] = $res['data'][0]['logisticsCompany'] ?? '';
                    //$order['promotion_pid'] = $res['data'][0]['merchantPid'] ?? '';
                    $order['source_trade_id'] = $res['data'][0]['sourceTradeId'] ?? '';
                    $order['activation_date'] = $res['data'][0]['activationDate'] ? strtotime($res['data'][0]['activationDate']) : '';
                    $order['recharge_date'] = $res['data'][0]['rechargeDate'] ? strtotime($res['data'][0]['rechargeDate']) : '';
                    $order['logistics_no'] = $res['data'][0]['logisticsNo'] ?? '';
                    $reportJHKey = $reportJHKey . $order['promotion_id'];
                    $reportCZKey = $reportCZKey . $order['promotion_id'];
                    switch ($res['data'][0]['tradeStatus']) {
                        // 充值成功
                        case Order::THIRD_ORDER_STATUS_CHARGED :
                            $order['status'] = Order::ORDER_STATUS_CHARGE_SUCCESS;
                            $order['charge_status'] = 1;
                            $order['active_status'] = 1;
                            $order['is_over'] = 1;
                            /*$redis->sadd($reportJHKey, $order['contact_phone']);
                            $redis->sadd($reportCZKey, $order['contact_phone']);*/
                            break;
                        // 首充成功
                        case Order::THIRD_ORDER_STATUS_FIRST_CHARGE :
                            $order['status'] = Order::ORDER_STATUS_CHARGE_FIRST_CHARGE;
                            $order['active_status'] = 1;
                            $cz_money = $order['charge_brokerage'];
                           /* $redis->sadd($reportJHKey, $order['contact_phone']);
                            $redis->sadd($reportCZKey, $order['contact_phone']);*/
                            break;
                        // 激活成功
                        case Order::THIRD_ORDER_STATUS_ACTIVE :
                            $order['status'] = Order::ORDER_STATUS_ACTIVE_SUCCESS;
                            $order['active_status'] = 1;
                           /* $jh_money = $order['active_brokerage'];
                            $redis->sadd($reportJHKey, $order['contact_phone']);*/
                            break;
                    }
                }


               /* $jsonData = array(
                    'trackCode' => $order['tracking_code'],// 跟踪号
                    'goodsType' => -1, // 商品类型
                    'orderTotalNum' => 0, // 订单总数
                    'yjMoney' => $jh_money + $cz_money, // 佣金金额
                );
                $redis->lpush($reportKey, json_encode($jsonData));*/
                unset($order['id']);
                (new Order())->where('id',$id)->save($order);

            }
        });
    }

}
